package com.cs171.market.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.cs171.market.Repository.PermissionRepository;
import com.cs171.market.entity.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author  yangyifei
 * @date  2020/06/19
 * @version 1.0
 */

@CrossOrigin
@RestController
public class PermissionController {

    @Autowired
    private PermissionRepository permissionRepository;

    /**
     * 1.查询全部的权限列表或树形列表
     * @return 多条数据
     */
    @GetMapping("/rights/{type}")
    public Object selectAll(@PathVariable("type")String type) {
        JSONObject pAll = jsonhead();
        List<Permission> data = permissionRepository.findAll();
        if(type.equals("list")){
            JSONArray result = (JSONArray) JSON.toJSON(data);
            pAll.put("data", result);
            //fastjson属性过滤器教程 https://blog.csdn.net/dianluo4096/article/details/102039860
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
            filter.getExcludes().add("children");
            return JSONObject.toJSONString(pAll,filter);
        }
        else if(type.equals("tree")){
            List<Permission> result = new ArrayList<>();
            //先找到所有0级分类
            for(Permission p : data){
                if(p.getPpid() == 0){
                    result.add(p);
                }
            }
            // 为0级分类添加子菜单，getChildren1是递归调用的
            for(Permission p : result){
                p.setPermissionList(getChildren1(p.getPid(), data));
            }
            // 输出
            JSONArray pTree = (JSONArray) JSON.toJSON(result);
            pAll.put("data", pTree);
        }
        return pAll;
    }

    /**
     * 递归查找子菜单
     *
     * @param id 当前权限id
     * @param rootList 要查找的列表
     * @return
     */
    private List<Permission> getChildren1(Integer id, List<Permission> rootList){
        //子菜单
        List<Permission> childList = new ArrayList<>();
        for(Permission p : rootList){
            // 遍历所有节点，将父菜单id与传过来的id比较
            if(p.getPpid().equals(id)){
                childList.add(p);
            }
        }
        // 把子菜单的子菜单再循环一遍
        for(Permission p : childList){
            p.setPermissionList(getChildren1(p.getPid(), rootList));
        }
        // 递归退出条件
        if (childList.size() == 0){
            return null;
        }
        return childList;
    }

    /**
     * 2.左侧菜单权限
     * @return 多条数据
     */
    @GetMapping("/menus")
    public Object selectLeft() {
        JSONObject pAll = jsonhead();
        List<Permission> data = permissionRepository.findAll();
        List<Permission> result = new ArrayList<>();
        //先找到所有0级分类
        for(Permission p : data){
            if(p.getPpid() == 0){
                result.add(p);
            }
        }
        // 为0级分类添加子菜单，getChildren1是递归调用的
        for(Permission p : result){
            p.setPermissionList(getChildren1(p.getPid(), data));
        }
        // 输出
        JSONArray pTree = (JSONArray) JSON.toJSON(result);
        pAll.put("data", pTree);
        pAll.getJSONObject("meta").put("msg","获取菜单列表成功");
        return pAll;
    }

    /**
     * 重构代码，相似的Json Head
     * @return JsonObject
     */
    private JSONObject jsonhead(){
        JSONObject pAll = new JSONObject();
        JSONObject pSuccess = new JSONObject();
        pSuccess.put("msg", "获取权限列表成功");
        pSuccess.put("status", "200");
        pAll.put("meta", pSuccess);
        return pAll;
    }
}
